import java.util.ArrayList;

/**
 * Created by asus on 2017/11/20.
 */
public class NetRouter<T> {
    private ArrayList<T> myList;
    private int[][] edges;
    private int NumEdges;
    public NetRouter(int n){
        myList = new ArrayList<>(n);
        edges = new int[n][n];
        setEdges(n);
        NumEdges = 0;
    }
    public void setEdges(int n){
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
                edges[i][j]=0;
            }
        }
    }
    public void insertNode(T item){
        myList.add(item);
    }
    public void removeNode(int position){
        myList.remove(position);
    }
    public void inserEdge(int a,int b,int weigth){
        if (!insexIsValid(a)||!insexIsValid(b)){
            throw new NumberFormatException("Wrong number");
        }
        edges[a][b] = weigth;
        vertexShortestDist();
        NumEdges++;
    }
    //判断值的大小是否正确
    public  boolean insexIsValid(int index){

        if (0<=index&&index<myList.size()){
            return true;
        }
        else return false;
    }
    public void vertexShortestDist(){
        for (int i=0;i<myList.size();i++){
                helpShortestDist(i);
        }
    }
    public void helpShortestDist(int index){
        for (int i=0;i<myList.size();i++){
            for (int j=0;j<myList.size();j++){
                if (edges[i][index]!=0&&edges[index][j]!=0){
                    if (edges[i][j]==0){
                    edges[i][j]=edges[i][index]+edges[index][j];}
                    else if (edges[i][j]>edges[i][index]+edges[index][j])
                        edges[i][j]=edges[i][index]+edges[index][j];
                }
            }
        }
    }
    public int GetShortDist(int a,int b){
        vertexShortestDist();
        return edges[a][b];
    }

}
